package com.vcourse.service.edu.controller.admin;


import com.vcourse.commons.result.R;
import com.vcourse.service.edu.entity.Video;
import com.vcourse.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author Zengsh123
 * @since 2020-05-25
 */
@Slf4j
@Api("课时管理")
@RestController
@RequestMapping("/admin/edu/video")
public class VideoController {
    @Resource
    private VideoService videoService;

    @ApiOperation("新增课时")
    @PostMapping("save")
    public R save(@ApiParam(value = "课时信息", required = true)
                  @RequestBody Video video) {
        boolean res = videoService.save(video);
        if (res) {
            return R.ok().message("保存课时成功");
        }
        return R.error().message("保存课时失败");
    }

    @ApiOperation("修改课时")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "课时信息", required = true)
                      @RequestBody Video video) {
        boolean res = videoService.updateById(video);
        if (res) {
            return R.ok().message("修改课时成功");
        }
        return R.error().message("修改课时失败");
    }

    @ApiOperation("根据id查询课时")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "课时id", required = true)
                         @PathVariable String id) {
        Video video = videoService.getById(id);
        if (video != null) {
            return R.ok().data(video);
        }
        return R.ok().message("此课时信息不存在或已被删除");
    }

    @ApiOperation("删除课时")
    @DeleteMapping("delete/{id}")
    public R deleteById(@ApiParam(value = "课时id", required = true)
                            @PathVariable String id) {
        //删除视频
        videoService.removeMediaVideoById(id);

        boolean res = videoService.removeById(id);
        if (res) {
            return R.ok().message("课时删除成功");
        }
        return R.error().message("数据不存在");
    }


}

